/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.data.crud;

import br.data.model.Listas;
import br.data.model.ListasPK;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author thiago
 */
public class CrudListas {

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("wabtPU");

    public void persist(Object object) {
        EntityManager em = emf.createEntityManager();
        try {
            em.getTransaction().begin();
            em.persist(object);
            em.getTransaction().commit();
        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", e);
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }

    public void atualiza(Object object) {
        EntityManager em = emf.createEntityManager();
        try {
            em.getTransaction().begin();
            em.merge(object);
            em.getTransaction().commit();
        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", e);
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }

    public int getNovoCodigoLista(int codigoCli) {
        EntityManager em = emf.createEntityManager();
        try {
            //int max = Integer.parseInt(em.createNamedQuery("Cliente.findMax").getResultList().get(0).toString());
            int max = Integer.parseInt(em.createQuery("SELECT MAX(l.listasPK.codigolist) FROM Listas l WHERE l.listasPK.codigocli = :codigocli").setParameter("codigocli", codigoCli).getResultList().get(0).toString());
            return (max + 1);
        } catch (Exception e) {
            return 0;
        } finally {
            em.close();
        }
    }

    public java.util.Collection<br.data.model.Listas> getAll() {

        EntityManager em = emf.createEntityManager();
        try {
            return em.createNamedQuery("Listas.findAll").getResultList();
        } catch (Exception e) {
            return null;
        } finally {
            em.close();
        }
    }

    public java.util.Collection<br.data.model.Listas> getAll(int codigocli) {

        EntityManager em = emf.createEntityManager();
        try {
            return em.createNamedQuery("Listas.findByCodigocli").setParameter("codigocli", codigocli).getResultList();
        } catch (Exception e) {
            return null;
        } finally {
            em.close();
        }
    }

    public java.util.Collection<br.data.model.Listas> getListasAbertas() {
        EntityManager em = emf.createEntityManager();
        try {
            java.util.Date dt = new java.util.Date();
            java.sql.Date sqlDate = new java.sql.Date(dt.getYear(), dt.getMonth(), dt.getDay());
            return em.createQuery("SELECT l FROM Listas l WHERE l.datafechamento >= CURRENT_DATE AND l.dataabertura <= CURRENT_DATE").getResultList();
        } catch (Exception e) {
            return null;
        } finally {
            em.close();
        }
    }

    public void remove(Listas lista) {
        EntityManager em = emf.createEntityManager();
        try {
            em.getTransaction().begin();
            Listas tmp = (Listas) em.merge(lista);
            em.remove(tmp);
            em.getTransaction().commit();
        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", e);
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }

    public Listas getLista(ListasPK pklista) {
        EntityManager em = emf.createEntityManager();
        try {
            Listas list = em.find(br.data.model.Listas.class, pklista);
            return list;
        } catch (Exception e) {
            return null;
        } finally {
            em.close();
        }
    }
}
